#include "4032.h"
#include "pcf8576.h"

I2C_HandleTypeDef hi2c1;

unsigned char  numSeg[] = {
	0xfd, 0xff, // 0
	0x00, 0xff, // 1
	0x7f, 0xfd, // 2
	0x1f, 0xff, // 3
	0xca, 0xff, // 4
	0xdf, 0xf7, // 5
	0xff, 0xf7, // 6
	0x0c, 0xff, // 7
	0xff, 0xff, // 8
	0xdf, 0xff, // 9
};

void i4031_drawNum(I2C_HandleTypeDef* i2cn, PCF8576_INIT init, uint8 * buffer, unsigned char index, unsigned char val) {
	if(index > 7) {
		return;
	}
	
	if(val > 9) {
		return;
	}
	
	pcf8576_drawByte(i2cn, init, buffer, index*2+1, numSeg[val*2]);
	pcf8576_drawByte(i2cn, init, buffer, index*2+2, numSeg[val*2+1]);
}

void i4031_full(I2C_HandleTypeDef* i2cn, PCF8576_INIT init, uint8 * buffer) {
	unsigned char i ;
	for(i = 0; i < 20; i++) {
		pcf8576_drawByte(i2cn, init, buffer, i, 0xff);
	}
}

void i4031_clear(I2C_HandleTypeDef* i2cn, PCF8576_INIT init, uint8 * buffer) {
	unsigned char i ;
	for(i = 0; i < 20; i++) {
		pcf8576_drawByte(i2cn, init, buffer, i, 0x00);
	}
}

void i4031_img1(I2C_HandleTypeDef* i2cn, PCF8576_INIT initStruct, uint8 * buffer) {
	unsigned char i;
	i4031_clear(i2cn, initStruct, buffer);
	for(i = 1; i < 10; i++){
		i4031_drawNum(i2cn, initStruct, buffer, i, 2);
	}
	pcf8576_drawPixel(i2cn, initStruct, buffer, 1, 0, 1);
	pcf8576_drawPixel(i2cn, initStruct, buffer, 1, 2, 1);
	
	pcf8576_drawPixel(i2cn, initStruct, buffer, 3, 1, 1);
	pcf8576_drawPixel(i2cn, initStruct, buffer, 3, 3, 1);
	
	pcf8576_drawPixel(i2cn, initStruct, buffer, 4, 0, 1);
	pcf8576_drawPixel(i2cn, initStruct, buffer, 4, 2, 1);
	
	pcf8576_drawPixel(i2cn, initStruct, buffer, 38, 0, 1);
	pcf8576_drawPixel(i2cn, initStruct, buffer, 38, 2, 1);
	
	pcf8576_drawPixel(i2cn, initStruct, buffer, 5, 2, 1);
	pcf8576_drawPixel(i2cn, initStruct, buffer, 5, 3, 1);
	pcf8576_drawPixel(i2cn, initStruct, buffer, 36, 3, 1);
	
	pcf8576_drawPixel(i2cn, initStruct, buffer, 34, 3, 1);
	pcf8576_drawPixel(i2cn, initStruct, buffer, 35, 3, 1);
}
void i4031_img2(I2C_HandleTypeDef* i2cn, PCF8576_INIT initStruct, uint8 * buffer){
	unsigned char i;
	i4031_clear(i2cn, initStruct, buffer);
	for(i = 1; i < 10; i++){
		i4031_drawNum(i2cn, initStruct, buffer, i, 4);
	}
	
	pcf8576_drawPixel(i2cn, initStruct, buffer, 1, 1, 1);
	pcf8576_drawPixel(i2cn, initStruct, buffer, 1, 3, 1);
	
	pcf8576_drawPixel(i2cn, initStruct, buffer, 2, 0, 1);
	pcf8576_drawPixel(i2cn, initStruct, buffer, 2, 2, 1);
	
	pcf8576_drawPixel(i2cn, initStruct, buffer, 4, 1, 1);
	pcf8576_drawPixel(i2cn, initStruct, buffer, 4, 3, 1);
	
	pcf8576_drawPixel(i2cn, initStruct, buffer, 38, 1, 1);
	pcf8576_drawPixel(i2cn, initStruct, buffer, 38, 3, 1);
	
	pcf8576_drawPixel(i2cn, initStruct, buffer, 36, 0, 1);
	
	pcf8576_drawPixel(i2cn, initStruct, buffer, 34, 2, 1);
	pcf8576_drawPixel(i2cn, initStruct, buffer, 35, 2, 1);
}
void i4031_img3(I2C_HandleTypeDef* i2cn, PCF8576_INIT initStruct, uint8 * buffer){
	unsigned char i;
	i4031_clear(i2cn, initStruct, buffer);
	for(i = 1; i < 10; i++){
		i4031_drawNum(i2cn, initStruct, buffer, i, 5);
	}

	pcf8576_drawPixel(i2cn, initStruct, buffer, 0, 0, 1);
	pcf8576_drawPixel(i2cn, initStruct, buffer, 0, 2, 1);

	pcf8576_drawPixel(i2cn, initStruct, buffer, 3, 1, 1);
	pcf8576_drawPixel(i2cn, initStruct, buffer, 3, 3, 1);

	pcf8576_drawPixel(i2cn, initStruct, buffer, 39, 0, 1);
	pcf8576_drawPixel(i2cn, initStruct, buffer, 39, 2, 1);

	pcf8576_drawPixel(i2cn, initStruct, buffer, 37, 0, 1);
	pcf8576_drawPixel(i2cn, initStruct, buffer, 37, 2, 1);

	pcf8576_drawPixel(i2cn, initStruct, buffer, 36, 1, 1);

	pcf8576_drawPixel(i2cn, initStruct, buffer, 34, 1, 1);
	pcf8576_drawPixel(i2cn, initStruct, buffer, 35, 1, 1);
}
void i4031_img4(I2C_HandleTypeDef* i2cn, PCF8576_INIT initStruct, uint8 * buffer){
	unsigned char i;
	i4031_clear(i2cn, initStruct, buffer);
	for(i = 1; i < 10; i++){
		i4031_drawNum(i2cn, initStruct, buffer, i, 7);
	}
	pcf8576_drawPixel(i2cn, initStruct, buffer, 0, 1, 1);
	
	pcf8576_drawPixel(i2cn, initStruct, buffer, 3, 0, 1);
	pcf8576_drawPixel(i2cn, initStruct, buffer, 3, 2, 1);
	
	pcf8576_drawPixel(i2cn, initStruct, buffer, 39, 1, 1);
	pcf8576_drawPixel(i2cn, initStruct, buffer, 39, 3, 1);
	
	pcf8576_drawPixel(i2cn, initStruct, buffer, 37, 1, 1);
	pcf8576_drawPixel(i2cn, initStruct, buffer, 37, 3, 1);
	
	pcf8576_drawPixel(i2cn, initStruct, buffer, 36, 2, 1);
	
	pcf8576_drawPixel(i2cn, initStruct, buffer, 34, 0, 1);
	pcf8576_drawPixel(i2cn, initStruct, buffer, 35, 0, 1);
}


void i4032LcdTest(I2C_HandleTypeDef* i2cn, PCF8576_INIT init, uint8 * buffer){
		i4031_full(i2cn, init, buffer);
	HAL_Delay(1000);
	
	i4031_img1(i2cn, init, buffer);
	HAL_Delay(1000);
	
	// "4"
	i4031_img2(i2cn, init, buffer);
	HAL_Delay(1000);
	
	// "5"
	i4031_img3(i2cn, init, buffer);
	HAL_Delay(1000);
	
	// "7"
	i4031_img4(i2cn, init, buffer);
	HAL_Delay(1000);
	
	
	
}

void i4032preTpTest(I2C_HandleTypeDef* i2cn, PCF8576_INIT init, uint8 * buffer){
	i4031_full(i2cn, init, buffer);
	for(unsigned char i = 0; i < 4; i++) {
		pcf8576_drawPixel(i2cn, init, buffer, 0, i, 0);
	}
	
	for(unsigned char i = 0; i < 4; i++) {
		pcf8576_drawPixel(i2cn, init, buffer, 1, i, 0);
	}
	
	pcf8576_drawPixel(i2cn, init, buffer, 34, 3, 0);
	pcf8576_drawPixel(i2cn, init, buffer, 35, 3, 0);
	
	pcf8576_drawPixel(i2cn, init, buffer, 5, 3, 0);
	pcf8576_drawPixel(i2cn, init, buffer, 5, 2, 0);
	pcf8576_drawPixel(i2cn, init, buffer, 36, 3, 0);
}



